package org.hackday.evetteam;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;

public class seeeventdetails extends Activity implements OnClickListener, OnItemClickListener {

	private ImageView icon;
	private TextView text;
	String i1,i2,i3,i4,i5,i6;
	private seeeventdetails ahp;
	private Button buttonView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdetail);
        
        icon = (ImageView) findViewById(R.id.icon);
        text = (TextView) findViewById(R.id.text);
        
       // downloadTwitterIcon();
       // downloadTwitterStream();
        
        Bundle extras = getIntent().getExtras();
         
         i2 = extras.getString( "eventname" );
         
        downloadEventsList();
        ahp=this;
        
        
        Context context; 
        int duration = Toast.LENGTH_SHORT;
		context = getApplicationContext();
		//Toast toast = Toast.makeText(context, url, duration);
		//toast.setGravity(Gravity.CENTER, 0, 0);
		//toast.show();
		
		buttonView = (Button) findViewById(R.id.createEvent);
        this.buttonView.setOnClickListener(this);
        
        buttonView = (Button) findViewById(R.id.createQR);
        this.buttonView.setOnClickListener(this);
    }
    
    
    public void downloadEventsList() {
		Handler handler = new Handler() {
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					Log.d("Twitter Icon", "Starting Connection");
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					String response = (String) message.obj;
					//text.setText(response);
					//Intent i = new Intent(this, EventViewActivity.class);
					String[] results = response.split( ",\\s*" );
					
					//name,creator,from,to,loc,eventname
					
					EditText myText = (EditText) ahp.findViewById(R.id.eventname);

					// Setting the text:
					myText.setText(results[4]);
					//String i1= myText.getText() ;
					myText = (EditText) ahp.findViewById(R.id.creatorname);
					myText.setText(results[0]);
					
					myText = (EditText) ahp.findViewById(R.id.vanue);
					myText.setText(results[3]);
					
					myText = (EditText) ahp.findViewById(R.id.starttime);
					myText.setText(results[1]);
					
					myText = (EditText) ahp.findViewById(R.id.endtime);
					myText.setText(results[2]);
					
					break;
				}
				case HttpConnection.DID_ERROR: {
					Exception e = (Exception) message.obj;
					e.printStackTrace();
					break;
				}
				}
			}
		};
		//http://localhost:8080/EventsServer/event.jsp?eventId=2&creatorId=2&from=aug3&to=aug4&location=MV&eventName=hackathon
		new HttpConnection(handler)
				.get("http://172.16.6.110:8080/EventsServer/eventDetail.jsp?eventName="+i2);
	}
    
    
    
    public void onClick(View v) {

		
		String text = "Whoa! You haven't selected your current stop. Please pick one from the list!";
		
		int duration = Toast.LENGTH_SHORT;
	 Context context; 
		context = getApplicationContext();
		Toast toast = Toast.makeText(context, text, duration);
		//toast.setGravity(Gravity.CENTER, 0, 0);
		//toast.show();
	
	//startActivity(i1);
		
		switch (v.getId()) {
		case R.id.createQR:
		
			 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
             intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
             startActivityForResult(intent, 0);
        break;
        
        
		case R.id.createEvent:
			Intent i1 = new Intent(this, MapAnnotateActivity.class);
			//Intent i1 = new Intent(this, ContactSelector.class);
			//i.putExtra(BusAdapter.STOP_ID, stopId);
			startActivity(i1);
            
		}
		
		
        
		
		}





public void onItemClick(AdapterView<?> arg0, View arg1, int listIndex,
	long arg3) {

}


public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	
	String text = "Whoa! You haven't selected your current stop. Please pick one from the list!";
	
	int duration = Toast.LENGTH_SHORT;
 
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            Context context; 
        	context = getApplicationContext();
        	Toast toast = Toast.makeText(context, contents, duration);
        	//toast.show();
        	
        	Intent i1 = new Intent(this, CheckinActivity.class);
			//Intent i1 = new Intent(this, ContactSelector.class);
			//i.putExtra(BusAdapter.STOP_ID, stopId);
			startActivity(i1);
        	
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}
	
	
}